import { Api } from '../../config/api.js'
import { request, WXNavigateTo } from '../../utils/index.js'
var appInst = getApp();

Page({
  data: {
    rate:'55.6',
    tabbar: {},
    last:0,
    lunbos: [
      {
          value: "阿尔巴斯羊1"
      },
      {
        value: "阿尔巴斯羊"
      }
    ],
    goodList: [], //会员专享
    tuition:[],//商城推荐
    host: appInst.globalData.host,
    banner:[], 
    goods:[],//搜索
    inpValue: "",
   isFocus: false,
    currentTabIndex:0,
    tabs: [{
            id: 0,
            iconName: 'vipzone_icon',
            text: '会员专享',
            isActive: true
        },
        {
            id: 1,
            iconName: 'vipzone_icon',
            text: '商城推荐',
            isActive: false
        }
    ],

    // 小圆点
     imgSource: ['url0', 'url1'],
    currentSwiper: 0,
  },

  
//   分页参数
   QueryParams: {
       page: 1,
       list: 3
   },


    // 声明定时器
    TimeId: -1,

  onLoad: function (options) {
    appInst.editTabbar(); //调用tabbar
    this._getGoodList()
    this._getSwipeList()
    this._getGoodtuition()
    this._sheepSite()
  },


  async _sheepSite(){
    const {data} = await request({
        url: Api.sheepSite,
        method: 'POST',
    })
    if(data){
       let data1 =  data.data.map(data=>{
           data.num = parseInt(data.buy_num/2000*100)
           return data
        })
        console.log(data1)
        console.log(data.data)
    this.setData({
        sheepSite:data.data
    })
    }
    console.log(data.data,'lingyang')
  },
// tab
 handleChangeTab: function (e) {
         const currentTabIndex = e.detail.tabIndex

         if (this.data.currentTabIndex === currentTabIndex) return

         this.setData({
             tabs: this.data.tabs.map(tab => ({
                 ...tab,
                 isActive: tab.id === currentTabIndex
             })),
             currentTabIndex
         })
 },




//   轮播
async _getSwipeList() {

    const {data} = await request({
        url: Api.getSwipeList,
        method: 'POST',
    })
        this.setData({
            banner:data.data
        })

    },


// 小圆点

 swiperChange(e) {
     this.setData({
         currentSwiper: e.detail.current
     })
 },


    // 跳转判断
    goodetail(e){
        const {id} =e.currentTarget.dataset
        const token = wx.getStorageSync("token")
        if(!token){
            wx.reLaunch({
                url: '/pages/login/login'
            });
        }else{
            wx.navigateTo({
                url: `/pages/good_detail/good_detail?goodid=${id}`
            });
             this.setData({
                 inpValue: "",
                 isFocus: false,
                 goods: []
             })
        }
        
    },

        // 会员专享
        async _getGoodList() {
            const {
                 data
            } = await request({
                url: Api.getGoodList,
                method: 'POST',
                data: {
                    tag: 2
                }
            })

            this.setData({
                goodList: data.data.data
            })
        },

    // 商品推荐  
        async _getGoodtuition() {
                const {data} = await request({
                    url: Api.getGoodList,
                    method: 'POST',
                    data: {
                        tag: 1,
                    }
                })
                // console.log(data)                                        
                this.setData({
                    tuition: data.data.data
                })      
        },
        async swipernav(){
            WXNavigateTo('/pages/sheep/sheep')
        },

           // 输入框
           handleInput(e) {
                   const {
                       value
                   } = e.detail;
                   // 2 检测合法性
                   if (!value.trim()) {
                       this.setData({
                           goods: [],
                           isFocus: false
                       })
                    return;
                   }
                    this.setData({
                        isFocus: true
                    })
                   // 停止上次的定时器
                   clearTimeout(this.TimeId);
                   this.TimeId =setTimeout(() => {
                       this.qsearch(value);
                   }, 1000);
               },
                // 搜索
               async qsearch(keyword) {
                   const {data} = await request({
                       url: Api.getGoodList,
                       data: {
                            tag: 1,
                            keyword
                       }
                   });
                   console.log(data)
                   this.setData({
                       goods: data.data.data
                   })
               },

                // 点击 取消按钮
                handleCancel() {
                    this.setData({
                            inpValue: "",
                            isFocus: false,
                            goods: []
                        })
                },

            // // 页面上滑 滚动条触底事件
            // onReachBottom() {
            //         const {
            //             last,
            //             goodList,
            //             tuition
            //         } = this.data
            //         if (!goodList.length || !tuition.length) return
            //         if (this.QueryParams.page >= last) {
            //             wx.showToast({
            //                 title: '没有下一页数据'
            //             });
            //         } else {
            //             this.QueryParams.page++;
            //              this._getGoodList();
            //              this._getGoodtuition()
            //         }
            //     },


                // // 下拉刷新
                // onPullDownRefresh() {
                //     // 1 重置数组
                //     this.setData({
                //         goodList: [],
                //         tuition:[]
                //     })
                //     // 2 重置页码
                //     this.QueryParams.page = 1;
                //     // 3 发送请求
                //     this._getGoodList();
                //     this._getGoodtuition()
                // },

})
